package controllers;

import aggregator.EsperAggregator;
import entities.Station;
import helpers.MathLink;
import play.Routes;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.index;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Application extends Controller {

    public static Result index() {
        sendStation();
        return ok(index.render(""));
    }

    public static Result sendEvent(String tag, String reader, Integer power,Integer button){

        Integer newPower;

        if (power != null)
            newPower = (4 - power)*10;
        else
            newPower = null;

        Map<String, Object> event = new HashMap<String, Object>();
        event.put("tag", Integer.valueOf(tag).toString());
        event.put("reader", reader);
        event.put("power", newPower);
        event.put("button", button);

        EsperAggregator.engine.getEPRuntime().sendEvent(event, "TagSighting");

        // Calculate distance to reader based on average signal strength in the last 10 seconds
//        DistanceCalculator distCalc = DistanceCalculator.getCalcs().get(reader);
//
//        Float distance = distCalc.calcDistance(newPower.floatValue());
//
//        // Add the current distance to the location estimator, which outputs a new location for every 3 reader seen in the last second
//        LocationCalculator.getCalc(tag).addDistance(reader, distance, distCalc.getX(), distCalc.getY());

        return ok();
    }

    public static Result sendStation(){


        Map<String, Object> event = new HashMap<String, Object>();
        List<Station> s = Station.getAllStations();
        for (Station station : s) {
            event.put("id", station.getId());
            event.put("x", station.getLocationX());
            event.put("y", station.getLocationY());
            event.put("name", station.getName());

            EsperAggregator.engine.getEPRuntime().sendEvent(event, "Stations");

            event.clear();
        }



        return ok();
    }


    public static Result getTagsPower(){
        return ok(Json.toJson(EsperAggregator.getCurrentEvents()));
    }

    public static Result getImage(){
        return ok(MathLink.getLinkInstance().getVisualiazedData()).as("image/gif");
    }

    public static Result jsRoutes(){
        return ok(
                Routes.javascriptRouter("jsRoutes",
                        routes.javascript.TagsController.getAllTags(),
                        routes.javascript.TagsController.getRealTimeTags(),
                        routes.javascript.StationsController.getAllStations(),
                        routes.javascript.ObjectsController.getAllObjects(),
                        routes.javascript.ObjectsController.saveObject(),
                        routes.javascript.StationsController.saveStation(),
                        routes.javascript.StationsController.deleteStation(),
                        routes.javascript.StationsController.getQuantitativeGraph(),
                        routes.javascript.StationsController.getAvgGraph(),
                        routes.javascript.StationsController.getObjectHistory(),
                        routes.javascript.Application.getTagsPower(),
                        routes.javascript.Application.getImage(),
                        routes.javascript.MessagesController.liveNotifications(),
                        routes.javascript.MessagesController.getLastTenNotifications(),
                        routes.javascript.MessagesController.setOldMessage(),
                        routes.javascript.Assets.at()
                )).as("text/javascript");
    }

    public static Result sendLocation(String tag, Integer x, Integer y){

        Map<String, Object> event = new HashMap<>();
        event.put("tag", tag);
        event.put("x", x);
        event.put("y", y);

        EsperAggregator.engine.getEPRuntime().sendEvent(event, "TagLocation");

        return ok(Json.toJson(EsperAggregator.mLocHelper));
    }
}